package com.scau.util;

import com.scau.exception.FileUtilException;
import com.scau.handleException.ResultCode;
import com.scau.pojo.Goods;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;
import sun.misc.Cleaner;

import java.io.File;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

/**
 * Siaoja
 */
public class FileUtil {

    public static boolean createDir(String fileDir){

        //创建用户各品牌的商品目录
        File dir = new File(fileDir);

        if(!dir.exists()){
            boolean mkdirs = dir.mkdirs();
            //循环，设置路径上的文件夹权限
            System.out.println("AbsolutePath:" + dir.getAbsolutePath());
            System.out.println("True of False:"+mkdirs);
            /*File tmp = dir;
            do{
                System.out.println("TMPAP" + tmp.getAbsolutePath());
                //chmodeFile(tmp);
                tmp=tmp.getParentFile();
            }while (tmp.getParentFile()!=null);
*/
        }

        return true;
    }

    public static void saveFile(MultipartFile img, String filePath) throws FileUtilException {
        File file = new File(filePath);
        System.out.println(file.getAbsolutePath());
        try {
            img.transferTo(file);
            //修改权限
            do{
                chmodeFile(file);
                file = file.getParentFile();
            }while (file.getParentFile()!= null);
        }catch (IOException e){
            throw new FileUtilException(ResultCode.FILE_SAVE_ERROR);
        }
        /*跨服务器上传文件
        System.out.println("FileUtil.filePath:" + filePath);
        Client client = Client.create();

        WebResource webResource = client.resource(filePath);
        webResource.put(img.getBytes());*/

    }
    public static void deleteFile(String filePath) throws FileUtilException {
        File file = new File(filePath);
        if(file.exists()){
            file.delete();
            System.out.println("FileUtil删除文件"+filePath+"成功");
        }else{
            throw new FileUtilException(ResultCode.FILE_DEL_ERROR);
        }
        /*跨服务器删除文件
        Client client = Client.create();
        WebResource webResource = client.resource(filePath);
        webResource.delete();*/

    }
    private static void chmodeFile(File file) throws IOException {
        //设置文件、目录权限为777
        Set<PosixFilePermission> permissionSet = new HashSet<PosixFilePermission>();
        permissionSet.add(PosixFilePermission.OWNER_READ);
        permissionSet.add(PosixFilePermission.OWNER_WRITE);
        permissionSet.add(PosixFilePermission.OWNER_EXECUTE);
        permissionSet.add(PosixFilePermission.GROUP_READ);
        permissionSet.add(PosixFilePermission.GROUP_WRITE);
        permissionSet.add(PosixFilePermission.GROUP_EXECUTE);
        permissionSet.add(PosixFilePermission.OTHERS_READ);
        permissionSet.add(PosixFilePermission.OTHERS_WRITE);
        permissionSet.add(PosixFilePermission.OTHERS_EXECUTE);

        Path path = Paths.get(file.getAbsolutePath());
        Files.setPosixFilePermissions(path,permissionSet);

    }
}
